import time

from selenium.webdriver.common.by import By

from base.mis.base_page import BasePage, BaseOperate
from utils import UtilsDriver, select_box


class ContentInspectPage(BasePage):
    def __init__(self):
        super().__init__()
        # 标题
        self.title = By.CSS_SELECTOR, ".el-input__inner"
        # 状态下拉框
        self.status_select = By.CSS_SELECTOR, "[readonly='readonly']"
        # 结束时间框
        self.end_time = By.CSS_SELECTOR, "[placeholder='选择结束时间']"
        # 查询按钮
        self.check_btn = By.CSS_SELECTOR, ".find"
        # 通过按钮
        self.adopt = By.XPATH, "//*[@class='el-table__row']/td[8]/div/button[2]"
        # 弹窗确定按钮
        self.confirm = By.XPATH, "//*[@class='el-message-box__btns']/button[2]/span"
        # 审核通过的文章名称
        self.adopt_article_name = By.XPATH, "//*[@class='el-table__body']/tbody/tr/td[2]/div"

    def find_title_box(self):
        return self.get_element(self.title)

    def find_check_btn(self):
        return self.get_element(self.check_btn)

    def find_adopt_btn(self):
        return self.get_element(self.adopt)

    def find_status_select(self):
        return self.get_element(self.status_select)

    def find_end_time(self):
        return self.get_element(self.end_time)

    def find_confirm_btn(self):
        return self.get_element(self.confirm)

    def find_adopt_article_name(self):
        return self.get_element(self.adopt_article_name)


class ContentInspectOperate(BaseOperate):
    def __init__(self):
        self.cip = ContentInspectPage()
        self.driver = UtilsDriver.get_mis_driver()

    def input_title(self, title):
        self.input_text(self.cip.find_title_box(), title)

    def click_check_btn(self):
        self.cip.find_check_btn().click()

    def click_adopt_btn(self):
        self.cip.find_adopt_btn().click()

    def select_status(self, status):
        select_box(self.driver, self.cip.find_status_select(), status)

    def input_end_time(self, end_time):
        self.input_text(self.cip.find_end_time(), end_time)

    def click_confirm_btn(self):
        self.cip.find_confirm_btn().click()

    def return_adopt_article_name(self):
        return self.cip.find_adopt_article_name().text


class ContentInspectBusiness:
    def __init__(self):
        self.cio = ContentInspectOperate()

    def adopt_article(self, title, status, end_time):
        self.cio.input_title(title)
        time.sleep(1)
        self.cio.select_status(status)
        time.sleep(1)
        self.cio.input_end_time(end_time)
        time.sleep(1)
        self.cio.click_check_btn()
        time.sleep(1)
        self.cio.click_adopt_btn()
        time.sleep(1)
        self.cio.click_confirm_btn()

    # 返回审核通过的文章名称
    def get_adopt_article_name(self, status):
        self.cio.select_status(status)
        self.cio.click_check_btn()
        return self.cio.return_adopt_article_name()
